package leetcode.median;

import leetcode.base.ListNode;

/**
 * ClassName: $92_ReverseBetween
 * Description:
 * date: 2021-12-14 9:34
 *
 * @author liyifan
 */
public class $92_ReverseBetween {

    ListNode help = null;

    public ListNode reverseBetween(ListNode head, int left, int right) {
        if (left == 1)
            return reverseN(head, right);
        head.next = reverseBetween(head.next, left - 1, right - 1);
        return head;
    }

    public ListNode reverseN(ListNode head, int n) {
        if (n == 1) {
            help = head.next;
            return head;
        }

        ListNode node = reverseN(head.next, n - 1);
        head.next.next = head;
        head.next = help;
        return node;
    }
}
